package com.wenjiang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wenjiang.common.dto.PageParam;
import com.wenjiang.common.exception.FileException;
import com.wenjiang.common.lang.Result;
import com.wenjiang.entity.MallCarousel;
import com.wenjiang.mapper.MallCarouselMapper;
import com.wenjiang.service.MallCarouselService;
import com.wenjiang.util.OSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
@Transactional
public class MallCarouselServiceImpl extends ServiceImpl<MallCarouselMapper,MallCarousel> implements MallCarouselService {

    @Autowired
    OSSUtils ossUtils;


    @Override
    public Result getCarousels() {
        List<MallCarousel> carousels =
                baseMapper.selectList(new QueryWrapper<MallCarousel>().eq("status",1).orderByDesc("carousel_rank"));


        return Result.succ(carousels);
    }




    @Override
    public Result getPageCarousels(PageParam pageParam) {

        int currentPage = pageParam.getCurrentPage();
        int pageSize = pageParam.getPageSize();

        // 计算页数
        int pageSite=(currentPage-1)*pageSize;

        Page<MallCarousel> page =new Page(currentPage, pageSize);

        Page page1 = page(page);

        List records = page1.getRecords();
        return Result.succ(records);
    }

    @Override
    public Result changeStatus(int carouselId, int status) {

        UpdateWrapper updateWrapper =new UpdateWrapper();

        updateWrapper.set("status", status);

        updateWrapper.eq("carousel_id", carouselId);

        boolean update = update(updateWrapper);

        if(update){
            return Result.succ("修改成功");
        }

        return Result.fail("系统错误");
    }


    @Override
    public Result updateCarousel(MallCarousel mallCarousel) {

        update(mallCarousel,new UpdateWrapper<MallCarousel>().eq("carousel_id", mallCarousel.getCarouselId()));

        return Result.succ("修改成功");
    }

    @Override
    public Result addCarousel(MallCarousel mallCarousel, MultipartFile file)  {

        //判断文件是否为空
        if(file==null){
            return Result.fail("上传的文件为空");
        }



        String name=file.getOriginalFilename();

        //文件后缀名
        String last = name.substring(name.lastIndexOf("."));

        if(!(last.equals(".jpg")||last.equals(".jpeg")||last.equals(".png"))){
            return Result.fail("文件类型不匹配");
        }

        // 插入数据
        save(mallCarousel);

        //获取文件名称 （轮播图id) +文件后缀名
        String filename=mallCarousel.getCarouselId()+name.substring(name.lastIndexOf("."));

         // 拼接上传路径
         String path="carouselImg/"+filename;



         try {
             ossUtils.upload(file.getBytes(), path);
             //设置url
             mallCarousel.setCarouselImgUrl(ossUtils.getUrl(path));

             //更新自己
             update(mallCarousel, new UpdateWrapper<MallCarousel>().eq("carousel_id", mallCarousel.getCarouselId()));

         }catch (Exception e){
             throw  new FileException();
         }
        return Result.succ("更新成功");
    }

    @Override
    public Result delCarousel(Integer carouselId) {

        // 根据自己的id查询图片
        MallCarousel mallCarousel = getById(carouselId);
        ossUtils.delFile(mallCarousel.getCarouselImgUrl());
        removeById(carouselId);
        return Result.succ("删除成功");
    }
}
